// 底部
if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}

// 初始化
function plusReady() {
    var subpages = ['../home/index.html', '../home/classification.html', '../coin/coinlist.html', '../shopcart/shoppingcart.html', '../mine/mylife.html'];
    var subpage_style = {top: '0px', bottom: '50px'};
    var aniShow = {}, haveInitPages = [];
    //当前激活选项
    var activeTab = subpages[0];

    var self = plus.webview.currentWebview();
    for (var i = 0; i < 5; i++) {
        var temp = {};
        var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);
        if (i > 0) {
            sub.hide();
        } else {
            haveInitPages.push(subpages[0]);
            temp[subpages[i]] = "true";
            $.extend(aniShow, temp);
            // 当前tab
            setLocalStorage(STORAGE_MANAGER.activeTab, subpages[0]);
        }
        self.append(sub);
    }
    
		getShopcartProductCount();

    // 绑定事件
    $("#footer").on("click", "li", function () {
        var targetTab = $(this).attr("tip");
        if (targetTab == activeTab) {
            return;
        }
				getShopcartProductCount();
        // 当前tab
        setLocalStorage(STORAGE_MANAGER.activeTab, targetTab);

        // 选中样式
        var activeli = $('#footer li[class=color-fff]');
        activeli.removeClass("color-fff");
        $(this).addClass("color-fff");
				
				console.log('targetTab:'+targetTab + '  activeTab:' + activeTab);
        if (os.ios) {
            if (targetTab == "../mine/mylife.html") {
                plus.navigator.setStatusBarBackground("#f08200");
                plus.navigator.setStatusBarStyle('UIStatusBarStyleBlackOpaque');
            } else if (activeTab == "../mine/mylife.html") {
                plus.navigator.setStatusBarBackground("#FEFEFE");
                plus.navigator.setStatusBarStyle('UIStatusBarStyleDefault');
            }
        }

				var currentLoginuser = getLocalStorage(STORAGE_MANAGER.user);
        // 第一次点击时候初始化页面
        if (haveInitPages.indexOf(targetTab) == -1) {
            haveInitPages.push(targetTab);
            switch (targetTab) {
                case "../home/index.html":
                    plus.webview.getWebviewById(targetTab).evalJS("indexPageReady()");
                    break;
                case "../home/classification.html":
                    plus.webview.getWebviewById(targetTab).evalJS("classificationPageReady()");
                    break;
                case "../coin/coinlist.html":
                    plus.webview.getWebviewById(targetTab).evalJS("coinlistPageReady()");
                    break;
                case "../shopcart/shoppingcart.html":									  
									  if (currentLoginuser == null) {
									  	redirect("../account/login.html?backurl=shopcart|shoppingcart.html");
									  }else{
                    	plus.webview.getWebviewById(targetTab).evalJS("shoppingcartPageReady()");
                   	}
                    break;
                case "../mine/mylife.html":
                    plus.webview.getWebviewById(targetTab).evalJS("mylifePageReady()");
                    break;
                default:
                    break;
            }
        } else {
          // 触发目标页
          if(null == currentLoginuser && "../shopcart/shoppingcart.html" == targetTab){
          	redirect("../account/login.html?backurl=shopcart|shoppingcart.html");
          }else{
          	plus.webview.getWebviewById(targetTab).evalJS("activePage()");
          }
        }

        //显示目标选项卡
        if (os.ios || aniShow[targetTab]) {
            plus.webview.show(targetTab);
        } else {
            var temp = {};
            temp[targetTab] = "true";
            $.extend(aniShow, temp);
            plus.webview.show(targetTab, "none", 300);
        }
        //隐藏当前;
        plus.webview.hide(activeTab);
        //更改当前活跃的选项卡
        activeTab = targetTab;
    });

    // 推送
    // 获取推送标识
    var info = plus.push.getClientInfo();

    // 监听点击消息事件
    plus.push.addEventListener("click", function (msg) {
        // 判断是从本地创建还是离线推送的消息
        switch (msg.payload) {
            case "LocalMSG":
                alert("点击本地创建消息启动：");
                break;
            default:
                alert("点击离线推送消息启动：");
                break;
        }
        // 提示点击的内容
        plus.nativeUI.alert(msg.content);

    }, false);
    // 监听在线消息事件
    plus.push.addEventListener("receive", function (msg) {
        if (msg.aps) {  // Apple APNS message
            alert("接收到在线APNS消息：");
        } else {
            alert("接收到在线透传消息：");
        }

    }, false);
}

// 跳转某个Tab
function redirectTab(tabIndex) {
		console.log('tabIndex:' + tabIndex);
		if(4 == tabIndex){
      plus.navigator.setStatusBarBackground("#f08200");
      plus.navigator.setStatusBarStyle('UIStatusBarStyleBlackOpaque');			
		}
    $($("#footer li")[tabIndex]).trigger("click");
    goHome();
}

/**
 * 返回主页关闭其他页
 * */
function goHome() {
    var viewlist = plus.webview.all();
    var exceptlist = ["../home/main.html", '../home/index.html', '../home/classification.html', '../coin/coinlist.html', '../shopcart/shoppingcart.html', '../mine/mylife.html'];
    for (var i = 0; i < viewlist.length; i++) {
        var webview = viewlist[i];
        console.log(webview.id);
        if (exceptlist.indexOf(webview.id) > -1) {
            // TODO
        } else {
            webview.close("none");
        }
    }
}

function getShopcartProductCount(){
	var count = getLocalStorage(STORAGE_MANAGER.cacheproductcount);
	if(count && count>0){
		$("#totalProductCount").show().html(count);
	}else{
		$("#totalProductCount").hide();
	}
}
